<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yundaapi/model')->方法名();
*/
namespace jy_yundaapi;
defined('BY_JYA') or exit('error');
class api extends \plugin{
	//查询
	public function query($orderid='',$mailno=''){
		$log=pdo_get('jy_yundaapi_log',array('mailno'=>$mailno,'uniacid'=>$_SESSION['uniacid'],"orderid"=>$orderid));
		if(!$log['lasttime'] || $log['lasttime']<TIMESTAMP){
			$log['id']=$this->subscribe($orderid,$mailno);
		}
		if($log['uptime'] && $log['uptime']+1800>TIMESTAMP){
			$steps=json_decode($log['steps'],true);
			return $steps;
		}
		$action='openapi/outer/logictis/query';
		$post=array(
			'mailno'=>$mailno,
		);
		$res=$this->request($action,$post);
		//print_r($res);exit;
		if(!$res['result']){
			exi('查询失败！'.$res['code'].$res['message'],'error');
		}
		if(!$res['data']['steps']){
			exi('查询失败！'.$res['code'].$res['remark'],'error');
		}else{
			$steps=$res['data']['steps'];
			foreach($steps as &$sv){
				$sv['step']=$sv['description'];
			}
			$statustype=array(
				'GOT'=>'已揽件',
				'TRANSIT'=>'运输中',
				'SIGNED'=>'已签收',
				'RETURN'=>'退回件',
				'SIGNFAIL'=>'异常签收',
			);
			if($log['id']){
				$status=$statustype[$res['data']['status']];
				pdo_update('jy_yundaapi_log',array('uptime'=>TIMESTAMP,"status" => $status,"remark" => $res['data']['remark'],'steps'=>ijson_encode($steps)),array('id'=>$log['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
        return $steps;
    }
	//订阅
	public function subscribe($orderid='',$mailno=''){
		$action='openapi/outer/logictis/subscribe';
		$orders=array();
		$orders[]=array('orderid'=>$orderid,'mailno'=>$mailno);
		$post=array(
			'orders'=>$orders,
		);
		$res=$this->request($action,$post);
		if(!$res['result']){
			 exi('订阅失败！'.$res['code'].$res['message'],'error');
		}
		//print_r($res);exit;
		if($res['data']){
			if($res['data']['orders'][0]['result'] || true){
				$log=pdo_get('jy_yundaapi_log',array('mailno'=>$mailno,'uniacid'=>$_SESSION['uniacid'],"orderid"=>$orderid));
				if($log['id']){
					pdo_update('jy_yundaapi_log',array('lasttime'=>TIMESTAMP+59*24*3600),array('id'=>$log['id'],'uniacid'=>$_SESSION['uniacid']));
				}else{
					$data=array(
						'uniacid'=>$_SESSION['uniacid'],
						"orderid"=>$orderid,
						"mailno" => $mailno,
						'lasttime'=>TIMESTAMP+59*24*3600,
						"createtime" => TIMESTAMP,
					);
					pdo_insert('jy_yundaapi_log',$data);
					$log['id']=pdo_insertid();
				}
			}else{
				 exi('订阅订单失败！','error');
			}
		}
        return $log['id'];
    }
    public function request($action,$post='',$msg=false){
        $setting=plugin('jy_yundaapi')->plugin_setting('basic');
		if(!$setting['isopen']){
		    if(!$noexi){
                exi('韵达接口未启用！','error');
            }else{
		        return false;
            }
        }
		if(!$setting['key'] || !$setting['secret']){
			exi('请联系管理员，设置基本参数！','error');
		}
		if($setting['isdemo']){
			$api_url='https://u-openapi.yundasys.com/';
		}else{
			$api_url='https://openapi.yundaex.com/';
		}
        $url = $api_url . $action;
		$json_info = json_encode($post,JSON_UNESCAPED_UNICODE);
		//生成的SIGN签名串
		$sign = md5($json_info.'_'.$setting['secret']);
		$extra=array(
			'Content-Type' => 'application/json',
			'app-key'=> $setting['key'],
			'sign'=> $sign,
			'req-time'=>TIMESTAMP,
		);
        $res=cfc('http')->ihttp_request($url,$json_info,$extra);
		if(is_error($res)){
			if($msg){
				return error($res['message']);
			}else{
				exi($res['message'].'，请求异常！','error');
			}
		}
		$res=json_decode($res['content'],true);
        return $res;
    }
}
?>